version: '3.9'

services:
  stats-db-init:
    image: postgres:14
    volumes:
      - ./stats-db-data:/var/lib/postgresql/data
    entrypoint:
      - sh
      - -c
      - |
        chown -R 2000:2000 /var/lib/postgresql/data

  stats-db:
    depends_on:
      stats-db-init:
        condition: service_completed_successfully
    image: postgres:14
    user: 2000:2000
    restart: always
    container_name: 'stats-postgres'
    command: postgres -c 'max_connections=200'
    environment:
        POSTGRES_DB: 'stats'
        POSTGRES_USER: 'stats'
        POSTGRES_PASSWORD: 'n0uejXPl61ci6ldCuE2gQU5Y'
    ports:
      - 7433:5432
    volumes:
      - ./stats-db-data:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U stats -d stats"]
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 10s

  stats:
    image: ghcr.io/blockscout/stats:${STATS_DOCKER_TAG:-latest}
    pull_policy: always
    platform: linux/amd64
    restart: always
    container_name: 'stats'
    depends_on:
      - "stats-db"
    extra_hosts:
      - 'host.docker.internal:host-gateway'
    env_file:
      -  ../envs/common-stats.env
    environment:
      - STATS__DB_URL=postgres://stats:n0uejXPl61ci6ldCuE2gQU5Y@stats-db:5432/stats
      - STATS__BLOCKSCOUT_DB_URL=${STATS__BLOCKSCOUT_DB_URL-postgresql://blockscout:ceWb1MeLBEeOIfk65gU8EjF8@db:5432/blockscout}
      - STATS__CREATE_DATABASE=true
      - STATS__RUN_MIGRATIONS=true
